package cz.muni.fi.pa165.transfers;

import cz.muni.fi.pa165.dto.DeliveryDTO;
import cz.muni.fi.pa165.dto.PackageDTO;
import cz.muni.fi.pa165.entity.Delivery;
import cz.muni.fi.pa165.entity.Package;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Class provides converting between Delivery entity and DTO
 * 
 * @author <a href="mailto:vramik at redhat.com">Vlastislav Ramik</a>
 */
public class DeliveryT {

    public static DeliveryDTO entityToDTO(Delivery delivery){
        if (delivery == null) {
            return null;
        }
        
        DeliveryDTO deliveryDTO = new DeliveryDTO();
        
        if (delivery.getCreationDate() != null) {
            deliveryDTO.setCreationDate(new Date(delivery.getCreationDate().getTime()));
        }
        if (delivery.getDeliveryDate() != null) {
            deliveryDTO.setDeliveryDate(new Date(delivery.getDeliveryDate().getTime()));
        }
        deliveryDTO.setCustomer(CustomerT.entityToDTO(delivery.getCustomer()));
        deliveryDTO.setDeliveryBoy(EmployeeT.entityToDTO(delivery.getDeliveryBoy()));
        deliveryDTO.setDeliveryNumber(delivery.getDeliveryNumber());
        deliveryDTO.setId(delivery.getId());
        List<PackageDTO> packagesDTO = new ArrayList<PackageDTO>();
        if(delivery.getPackages() != null) {
            for (Package pkg : delivery.getPackages()) {
                packagesDTO.add(PackageT.entityToDTO(pkg));
            }
        }
        deliveryDTO.setPackages(packagesDTO);
        deliveryDTO.setRecipientAddress(delivery.getRecipientAddress());
        deliveryDTO.setRecipientCity(delivery.getRecipientCity());
        deliveryDTO.setRecipientName(delivery.getRecipientName());
        deliveryDTO.setRecipientZip(delivery.getRecipientZip());
        
        return deliveryDTO;        
    }
    
    public static Delivery dTOtoEntity(DeliveryDTO deliveryDTO){
        if (deliveryDTO == null) {
            return null;
        }
        
        Delivery delivery = new Delivery();
        
        if (deliveryDTO.getCreationDate() != null) {
            delivery.setCreationDate(new Date(deliveryDTO.getCreationDate().getTime()));
        }
        if (deliveryDTO.getDeliveryDate() != null) {
            delivery.setDeliveryDate(new Date(deliveryDTO.getDeliveryDate().getTime()));
        }
        delivery.setCustomer(CustomerT.dTOtoEntity(deliveryDTO.getCustomer()));
        delivery.setDeliveryBoy(EmployeeT.dTOtoEntity(deliveryDTO.getDeliveryBoy()));
        delivery.setId(deliveryDTO.getId());
        List<Package> packages = new ArrayList<Package>();
        if(deliveryDTO.getPackages() != null) {
            for (PackageDTO pkgDTO : deliveryDTO.getPackages()) {
                packages.add(PackageT.dTOtoEntity(pkgDTO));
            }
        }
        delivery.setPackages(packages);
        delivery.setRecipientAddress(deliveryDTO.getRecipientAddress());
        delivery.setRecipientCity(deliveryDTO.getRecipientCity());
        delivery.setRecipientName(deliveryDTO.getRecipientName());
        delivery.setRecipientZip(deliveryDTO.getRecipientZip());
        
        return delivery;     
    }    
}
